package com.example.service.impl;

import com.example.entity.UserNoticeRead;
import com.example.mapper.UserNoticeReadMapper;
import com.example.mapper.NoticeMapper;
import com.example.service.UserNoticeReadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户通知阅读记录服务实现类
 */
@Service
public class UserNoticeReadServiceImpl implements UserNoticeReadService {

    @Autowired
    private TransactionTemplate transactionTemplate;
    
    @Autowired
    private UserNoticeReadMapper userNoticeReadMapper;
    
    @Autowired
    private NoticeMapper noticeMapper;
    
    @Override
    @Transactional
    public boolean markAsRead(Long userId, Long noticeId) {
        try {
            // 检查是否已经标记为已读
            UserNoticeRead existing = userNoticeReadMapper.selectByUserIdAndNoticeId(userId, noticeId);
            if (existing != null) {
                return true; // 已经标记过了
            }
            
            // 创建新的阅读记录
            UserNoticeRead readRecord = new UserNoticeRead(userId, noticeId);
            int result = userNoticeReadMapper.insert(readRecord);
            return result > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    @Override
    @Transactional
    public boolean batchMarkAsRead(Long userId, List<Long> noticeIds) {
        try {
            if (noticeIds == null || noticeIds.isEmpty()) {
                return true;
            }
            
            // 过滤掉已经标记为已读的通知
            List<Long> readNoticeIds = getReadNoticeIds(userId);
            List<Long> unreadNoticeIds = noticeIds.stream()
                    .filter(id -> !readNoticeIds.contains(id))
                    .collect(Collectors.toList());
            
            if (unreadNoticeIds.isEmpty()) {
                return true;
            }
            
            int result = userNoticeReadMapper.batchInsert(userId, unreadNoticeIds);
            return result > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    @Override
    public boolean isRead(Long userId, Long noticeId) {
        UserNoticeRead readRecord = userNoticeReadMapper.selectByUserIdAndNoticeId(userId, noticeId);
        return readRecord != null;
    }
    
    @Override
    public List<Long> getReadNoticeIds(Long userId) {
        return userNoticeReadMapper.selectReadNoticeIdsByUserId(userId);
    }
    
    @Override
    public long getUnreadCount(Long userId) {
        return userNoticeReadMapper.selectUnreadCountByUserId(userId);
    }
    
    @Override
    @Transactional
    public boolean markAllAsRead(Long userId) {
        try {
            // 获取所有已发布的通知ID
            List<Long> allPublishedNoticeIds = noticeMapper.selectAllPublishedNoticeIds();
            
            if (allPublishedNoticeIds.isEmpty()) {
                return true;
            }
            
            return batchMarkAsRead(userId, allPublishedNoticeIds);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
